package co.fitstart.admin.web.controller.report;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import com.demo2do.core.utils.CollectionUtils;
import com.demo2do.core.utils.DateFormatUtils;
import com.demo2do.core.utils.DateUtils;
import com.demo2do.core.utils.StringUtils;

import co.fitstart.admin.entity.security.Login;
import co.fitstart.admin.service.CoachService;
import co.fitstart.admin.service.PrincipalService;
import co.fitstart.admin.service.ReportService;
import co.fitstart.admin.service.SiteService;
import co.fitstart.admin.service.StudentService;
import co.fitstart.admin.web.interceptor.MenuSetting;
import co.fitstart.admin.web.resolver.Secure;
import co.fitstart.admin.web.view.ExcelTemplateView;
import co.fitstart.entity.principal.Principal;
import co.fitstart.entity.site.Site;
import co.fitstart.entity.user.Student;
import co.fitstart.entity.user.bwh.BwhRecord;

/**
 * 三围数据报表
 * @author haiyan_xiao
 *
 */
@Controller
@RequestMapping("/bwh-reports")
@MenuSetting("menu-report")
public class BwhReportController {
    @Autowired
    private SiteService siteService;
    
    @Autowired
    private CoachService coachService;
    
    @Autowired
    private ReportService reportService;
    
    @Autowired
    private StudentService studentService;
    
    @Autowired
    private PrincipalService principalService;
    
    /**
     * 
     * @param login
     * @return
     */
    @RequestMapping(value = "", method = RequestMethod.GET)
    @MenuSetting("submenu-bwh-report")
    public ModelAndView list(@Secure Login login) {
        
        ModelAndView modelAndView = new ModelAndView("report/bwh-report");
        
        return modelAndView;
    }
    
    /**
     * 
     * @param login
     * @param siteId
     * @param mobile
     * @param coachId
     * @param from
     * @param to
     * @return
     */
    @RequestMapping(value = "/search", method = RequestMethod.GET)
    @MenuSetting("submenu-bwh-report")
    public ModelAndView onSearch(@Secure Login login, 
                                 @RequestParam(name = "site", required = false) Long siteId,
                                 @RequestParam(name = "mobile", required = false) String mobile,
                                 @RequestParam(name = "coach", required = false) Long coachId,
                                 @RequestParam(name = "from", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd")Date from,
                                 @RequestParam(name = "to", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd")Date to) {
        ModelAndView modelAndView = new ModelAndView("report/bwh-report");
        
        Site site = null;
        if(login.hasSite()) {
            site = login.getSite();
        } else if(siteId != null) {
            site = siteService.load(siteId);
            if(site != null) {
                modelAndView.addObject("selectedSiteId", site.getId());
            }
        }
        
        mobile = StringUtils.isBlank(mobile) ? null :mobile.trim();
        
        Principal coach = null;
        if(coachId != null) {
            coach = coachService.load(coachId);
        }
        
        if(to != null) {
            to = DateUtils.addDays(to, 1);
        }
        
        Map<String, List<BwhRecord>> bwhRecords = new HashMap<>();
        
        if(login.isAreaManager() || login.isAreaServer()) {
            Principal principal = principalService.load(Principal.class, login.getId());
            
            List<Site> sites = siteService.listSite(principal);
            
            if(!sites.isEmpty()) {
                if(siteId == null) {
                    bwhRecords = reportService.getBwhReport(sites, mobile, coach, from, to);
                } else if(site != null && sites.contains(site)) {
                    bwhRecords = reportService.getBwhReport(site, mobile, coach, from, to);
                }
            }
        } else {
            bwhRecords = reportService.getBwhReport(site, mobile, coach, from, to);
        }
        
        modelAndView.addObject("bwhRecords", bwhRecords);
        modelAndView.addObject("search", true);
        
        return modelAndView;
    }
    
    /**
     * 
     * @param login
     * @param siteId
     * @param mobile
     * @param coachId
     * @param from
     * @param to
     * @return
     */
    @RequestMapping(value = "/export", method = RequestMethod.GET)
    public ModelAndView onExport(@Secure Login login, 
                                 @RequestParam(name = "site", required = false) Long siteId,
                                 @RequestParam(name = "mobile", required = false) String mobile,
                                 @RequestParam(name = "coach", required = false) Long coachId,
                                 @RequestParam(name = "from", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd")Date from,
                                 @RequestParam(name = "to", required = false) @DateTimeFormat(pattern = "yyyy-MM-dd")Date to) {
        ModelAndView modelAndView = new ModelAndView(new ExcelTemplateView("bwh-report.xls"));
        
        Site site = null;
        if(login.hasSite()) {
            site = login.getSite();
        } else if(siteId != null) {
            site = siteService.load(siteId);
        }
        
        mobile = StringUtils.isBlank(mobile) ? null :mobile.trim();
        
        Principal coach = null;
        if(coachId != null) {
            coach = coachService.load(coachId);
        }
        
        if(to != null) {
            to = DateUtils.addDays(to, 1);
        }
        
        Map<String, List<BwhRecord>> bwhRecords = new HashMap<>();
        
        if(login.isAreaManager() || login.isAreaServer()) {
            Principal principal = principalService.load(Principal.class, login.getId());
            
            List<Site> sites = siteService.listSite(principal);
            
            if(!sites.isEmpty()) {
                if(siteId == null) {
                    bwhRecords = reportService.getBwhReport(sites, mobile, coach, from, to);
                } else if(site != null && sites.contains(site)) {
                    bwhRecords = reportService.getBwhReport(site, mobile, coach, from, to);
                }
                
            }
        } else {
            bwhRecords = reportService.getBwhReport(site, mobile, coach, from, to);
        }
        
        if(site != null) {
            modelAndView.addObject("site", site.getName());
        }
        modelAndView.addObject("mobile", mobile);
        if(coach != null) {
            modelAndView.addObject("coach", coach.getName());
        }
        if(from != null) {
            modelAndView.addObject("from", DateFormatUtils.format(from, "yyyy-MM-dd"));
        }
        if(to != null) {
            modelAndView.addObject("to", DateFormatUtils.format(DateUtils.addDays(to, -1), "yyyy-MM-dd"));
        }
        modelAndView.addObject("bwhRecords", bwhRecords);
        
        Set<String> mobiles = bwhRecords.keySet();
        
        if(!mobiles.isEmpty()) {
            List<Student> students = studentService.list(mobiles);
            Map<String, Student> studentsMap = CollectionUtils.constructMapValueFromList(students, "mobile");
            
            modelAndView.addObject("students", studentsMap);
        }
        
        return modelAndView;
    }
}
